package com.qifan.energy.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qifan.energy.adapter.RecommendRelationAdapter;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.result.MemberInfo;
import com.qifan.energy.bean.result.RecommendRelationResult;
import com.qifan.energy.R;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.CheckUtil;
import com.qifan.energy.util.DateTimeUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyRecommendActivity extends BaseActivity {

    @BindView(R.id.super_name_tv)
    TextView superNameTv;
    @BindView(R.id.super_add_tv)
    TextView superAddTv;
    @BindView(R.id.reach_tv)
    TextView reachTv;
    @BindView(R.id.accumulation_time_tv)
    TextView accumulationTimeTv;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.application_tv)
    TextView applicationTv;
    @BindView(R.id.recommand_num_tv)
    TextView recommandNumTv;
    RecommendRelationAdapter adapter;
    private List<RecommendRelationResult.ChildListBean> dataList = new ArrayList();
    private RecommendRelationResult.ParentListBean memberInvitation = null;

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_recommend;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        /* 获取本地用户信息 */
        MemberInfo result = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        if (result.getIsPartner() == 1) {//合伙人 不显示
            applicationTv.setVisibility(View.GONE);
        } else {//普通人 显示
            if (result.getPrePartner() == 1){//准合伙人
                applicationTv.setVisibility(View.VISIBLE);
            } else {//普通用户
                applicationTv.setText("未成为事业合伙人");
            }
        }
        adapter = new RecommendRelationAdapter(dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .sizeResId(R.dimen.dp_1)
                .colorResId(R.color.divider_color)
                .build());
    }

    @Override
    protected void initData() {
        NetUtils.memberCenterContacts(new MyObserver<RecommendRelationResult>(this) {
            @Override
            public void onSuccess(RecommendRelationResult result) {
                List<RecommendRelationResult.ParentListBean> parentList = result.getParentList();
                if (CheckUtil.hasContent(parentList)){
                    RecommendRelationResult.ParentListBean superBean = parentList.get(0);
                    String dateStr = DateTimeUtils.getDateStr(superBean.getCreateTime(),null);
                    superNameTv.setText(superBean.getNickname());
                    superAddTv.setText(String.format("加入时间：%s",dateStr));
                    accumulationTimeTv.setText(String.format("累计时间(分钟)：%.02f",superBean.getExpendTimeCard()));
                    if (superBean.getIsReached() == 0){
                        reachTv.setText("未完成");
                        reachTv.setBackgroundResource(R.drawable.shape_gray_r10);
                    } else {
                        reachTv.setText("已完成");
                        reachTv.setBackgroundResource(R.drawable.shape_red_r10);
                    }
                }
                List<RecommendRelationResult.ChildListBean> childList = result.getChildList();
                if (CheckUtil.hasContent(childList)) {
                    int reachCount = 0;
                    for (RecommendRelationResult.ChildListBean item: childList) {
                        if (item.getIsReached()>0) {
                            ++reachCount;
                        }
                    }
                    recommandNumTv.setText(String.format("(共%d人，已完成60分钟%d人)",childList.size(),reachCount));
                }
                adapter.setNewData(childList);

                //自己信息
                if (result.getMemberInvitation() != null) {
                    memberInvitation = result.getMemberInvitation();
                    MemberInfo memberInfo = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
                    if (memberInfo.getIsPartner() == 1) {//合伙人 不显示
                        applicationTv.setVisibility(View.GONE);
                    } else {//普通人 显示
                        if (memberInvitation.getIsReached()>0){
                            if (memberInfo.getPrePartner() == 1){//准合伙人
                                applicationTv.setVisibility(View.VISIBLE);
                            } else {//普通用户
                                applicationTv.setText("未成为事业合伙人");
                            }
                        } else {
                            applicationTv.setText("您还未玩够60分钟");
                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.application_tv)
    void onApplicationTvClicked(TextView textView){
        MemberInfo memberInfo = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        if (memberInfo.getPrePartner() == 1) {//准合伙人
            NetUtils.ssoApplyPartner(new MyObserver(this) {
                @Override
                public void onSuccess(Object result) {
                    textView.setClickable(false);
                    ToastUtils.showShort("您已申请");
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    textView.setClickable(false);
                    ToastUtils.showShort(errorMsg);
                }
            });
        } else {//普通用户
            if (memberInvitation != null){
                if (memberInvitation.getIsReached()>0){
                    ToastUtils.showShort("您还不满足申请合伙人条件哦~");
                } else {
                    ToastUtils.showShort("您还未玩够60分钟");
                }
            } else {
                ToastUtils.showShort("您还不满足申请合伙人条件哦~");
            }
        }
    }
}
